//
//  WDDFinancialView.m
//  SZBank
//
//  Created by rimi on 16/7/8.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "WDDFinancialView.h"
#import "WDDFinancialTableViewCell.h"
#import "WDDFlipView.h"
#import "NetWorkingDataManager.h"
#import "LocalDataManager.h"
@interface WDDFinancialView()<UITableViewDataSource,UITableViewDelegate,UIScrollViewAccessibilityDelegate>
{
    CGFloat frameWidth;/**  自身宽度 */
    CGFloat frameHeight;/**  自身高度 */
    NSMutableArray *headImageArray;/**  广告图片数组 */
    NSMutableArray *contentImageArray;/**  内容图片数组 */
    NSMutableArray *title;/**  广告标题数组 */
    NSTimer *timer;
    NSInteger _imageIndex;
}
//表格框架
@property (nonatomic,strong) UITableView *mainTableView;
//滚动视图
@property (nonatomic,strong) UIScrollView *scrollView;
//分页控制器
@property (nonatomic,strong) UIPageControl *pageController;

@property (nonatomic,strong) WDDFlipView *flipView;

@end
@implementation WDDFinancialView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        frameWidth = self.frame.size.width;
        frameHeight = self.frame.size.height;
        title = [NSMutableArray array];
        headImageArray = [NSMutableArray array];
        contentImageArray = [NSMutableArray array];
        [self addSubview:self.backgroundImage];
        UIImageView *imageLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, frameHeight * 0.90, frameWidth, frameHeight * 0.05)];
        imageLine.image = [LocalDataManager getImageWithSourceOfPath:@"金融资讯_底边"];
        [self addSubview:imageLine];
        [self addSubview:self.mainTableView];
        /**  广告数据请求 */
        [NetWorkingDataManager getProtocolAdvertisementWithParam:nil success:^(id sender) {
            NSInteger index = 0;
            for (NSDictionary *dictionary in sender[@"datalist"]){
                if (!(index == 2) && !(index == 5)){
                    [title addObject:[dictionary objectForKey:@"TITLE"]];
                    [headImageArray addObject:[dictionary objectForKey:@"THUMBNAILURL"]];
                    [contentImageArray addObject:[dictionary objectForKey:@"DETAILSURL"]];
                }
                if (index > 5){
                    break;
                }
                index ++;
            }
            [self initializeUserInterface];
        } failure:nil];
        timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(action_timer) userInfo:nil repeats:YES];
    }
    return self;
}

- (void)initializeUserInterface{
    for (int i = 0; i < 5; i ++) {
        /**  通过地址获取网络图片 */
        UIImage *topImage = [LocalDataManager getImageWithURL:headImageArray[i]];
        UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake(i * frameWidth * 0.96, 0, frameWidth * 0.96, frameHeight * 0.35)];
        /**  标题 */
        [imageButton setBackgroundImage:topImage forState:UIControlStateNormal];
        [imageButton addTarget:self action:@selector(action_imageButton:) forControlEvents:UIControlEventTouchUpInside];
        imageButton.tag = 1000 + i;
        [self.scrollView addSubview:imageButton];
    }
    _imageIndex = 1;
}

- (void)action_imageButton:(UIButton *)sender{
    self.flipView.titleLabel.text = title[sender.tag - 1000];
    if (!((sender.tag - 1000) == 2)){
        self.flipView.contentImage.hidden = NO;
        self.flipView.contentImage.image = [LocalDataManager getImageWithURL:contentImageArray[sender.tag - 1000]];
    }
    else{
        self.flipView.contentImage.hidden = YES;
    }
    self.flipView.alpha = 0;
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:self.flipView];
    /**  弹出动画 */
    [UIView animateWithDuration:1 animations:^{
        [self.flipView addSubview:self.flipView.contentImage];
        self.flipView.alpha = 1;
    } completion:nil];
}
#pragma mark -- 计时器循环滚动
- (void)action_timer{
    [self.scrollView setContentOffset:CGPointMake(_imageIndex * ((int)(frameWidth * 0.96)), 0) animated:YES];
    _imageIndex ++;
    if (_imageIndex == 6) {
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        _imageIndex = 1;
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    timer.fireDate = [NSDate distantFuture];
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    timer.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int page = self.scrollView.contentOffset.x / (int)(frameWidth * 0.96);
    self.pageController.currentPage = page;
}

/**  tableView代理方法 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return KHEIGHT * 0.4;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WDDFinancialTableViewCell *cell = [[WDDFinancialTableViewCell alloc] init];
    if (indexPath.row == 0) {
        [cell.contentView addSubview:cell.newsView];
        
        cell.financialOfLogo.image = [LocalDataManager getImageWithSourceOfPath:@"金融资讯_新闻分类"];
        cell.financialOfTitle.text = @"重要新闻";
        cell.directionImage.image = [LocalDataManager getImageWithSourceOfPath:@"右滑"]; 
    }
    if (indexPath.row == 1) {
        
        [cell.contentView addSubview:cell.productsView];
        cell.financialOfLogo.image = [LocalDataManager getImageWithSourceOfPath:@"金融资讯_新推分类"];
        cell.financialOfTitle.text = @"热推产品";
        cell.directionImage.image = [LocalDataManager getImageWithSourceOfPath:@"右滑"];
    }
    if (indexPath.row == 2) {
        [cell.contentView addSubview:cell.tableView];
        cell.financialOfLogo.image = [LocalDataManager getImageWithSourceOfPath:@"金融资讯_公告分类"];
        cell.financialOfTitle.text = @"最新公告";
        cell.directionImage.image = [LocalDataManager getImageWithSourceOfPath:@"下滑"];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UITableView *)mainTableView{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(frameWidth * 0.02, frameHeight * 0.02, frameWidth * 0.96, frameHeight * 0.90)];
        _mainTableView.dataSource = self;
        _mainTableView.delegate = self;
        _mainTableView.backgroundColor = [UIColor clearColor];
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        //注册cell
        [_mainTableView registerClass:[WDDFinancialTableViewCell class] forCellReuseIdentifier:@"cellIdentifier"];
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frameWidth , frameHeight * 0.4)];
        headerView.backgroundColor = [UIColor clearColor];
        [headerView addSubview:self.scrollView];
        [headerView addSubview:self.pageController];
        _mainTableView.tableHeaderView = headerView;
        
    }
    return _mainTableView;
}

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frameWidth * 0.96, frameHeight * 0.35)];
        _scrollView.backgroundColor = [UIColor clearColor];
        _scrollView.contentSize = CGSizeMake(frameWidth * 0.96 * 5, frameHeight * 0.35);
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
    }
    return _scrollView;
}

- (UIPageControl *)pageController{
    if (!_pageController) {
        _pageController = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, frameWidth * 0.04, frameHeight * 0.02)];
        _pageController.center = CGPointMake(self.center.x - frameWidth * 0.15, CGRectGetMaxY(self.scrollView.frame) + 20);
        _pageController.numberOfPages = 5;
        _pageController.currentPage = 0;
        _pageController.pageIndicatorTintColor = [UIColor blackColor];
        _pageController.currentPageIndicatorTintColor = [UIColor redColor];
    }
    return _pageController;
}

- (UIImageView *)backgroundImage{
    if (!_backgroundImage) {
        _backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frameWidth, frameHeight)];
        _backgroundImage.image = [LocalDataManager getImageWithSourceOfPath:@"背景"];
    }
    return _backgroundImage;
}

- (WDDFlipView *)flipView{
    if (!_flipView) {
        _flipView = [[WDDFlipView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT)];
    }
    return _flipView;
}
@end

